package cs342.uwosh.edu.QuizApp.thread;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.res.AssetManager;
import android.content.res.XmlResourceParser;

import cs342.uwosh.edu.QuizApp.storage.FBQuestion;
import cs342.uwosh.edu.QuizApp.storage.MCQuestion;
import cs342.uwosh.edu.QuizApp.storage.Question;
import cs342.uwosh.edu.QuizApp.storage.Quiz;

public class QuizXMLParser {
	private String TF = "questionTF";
	
	public QuizXMLParser() {}
	
	public QuizXMLParser(AssetManager manager) {
		//TODO create validator based off of quiz.dtd
	}
	
	public Quiz buildQuiz(File xmlFile) {
		SAXBuilder builder = new SAXBuilder();
		Document document = null;
		try {
			document = (Document) builder.build(xmlFile);
		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}
		return buildQuiz(document);
	}
	
	public Quiz buildQuiz(String rawXML) {
		
		SAXBuilder builder = new SAXBuilder();
		Document document = null;
		try {
			document = (Document) builder.build(new StringReader(rawXML));
		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}
		return buildQuiz(document);
		
		
	}
	
	public Quiz buildQuiz(Document document) {
		Quiz quiz = null;
		Element rootNode = document.getRootElement();
		quiz = new Quiz(rootNode.getAttributeValue("name"),
				rootNode.getAttributeValue("course"),
				rootNode.getChild("descr").getText());
		
		List<Element> list = new ArrayList<Element>();
		list.addAll(rootNode.getChildren("questionfb"));
		list.addAll(rootNode.getChildren("questionmc"));
		list.addAll(rootNode.getChildren("questiontf"));
		
		for(Element q : list) {
			quiz.addQuestion(buildQuestion(q));
		}
		return quiz;
	}
	
	
	
	public Question buildQuestion(Element e) {
		Question q = null;
		
		if("questionfb".equalsIgnoreCase(e.getName())) {
			q = new FBQuestion(e.getChild(Quiz.DESCR).getText(), e.getChild(Quiz.KEY).getText());
		} else if("questionmc".equalsIgnoreCase(e.getName())) {
			
			List<Element> elements = e.getChildren(Quiz.NONKEY);
			ArrayList<String> choices = new ArrayList<String>();
			
			for(Element e1 : elements) {
				choices.add(e1.getText());
			}
			
			String answer = e.getChild(Quiz.KEY).getText();
			choices.add(answer);
			
			q = new MCQuestion(e.getChild(Quiz.DESCR).getText(), 
					choices,
					answer);
		} else { //questiontf
			ArrayList<String> choices = new ArrayList<String>(2);
			choices.add(new String("True"));
			choices.add(new String("False"));
			q = new MCQuestion(e.getChild(Quiz.DESCR).getText(), choices, e.getChild(Quiz.KEY).getText());
		}
		return q;
	}
}
